package com.java.admin.makeanappointment.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.database.entity.Makeanappointmentinfo;
import com.java.database.entity.Servicetype;
import com.java.database.mapper.MakeanappointmentinfoMapper;
import com.java.database.mapper.ServicetypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Service
public class MakeanAppointmentService {
    @Autowired
    private MakeanappointmentinfoMapper makeanappointmentinfoMapper;
    @Autowired
    private ServicetypeMapper servicetypeMapper;
    /**
     *  分页查询所有的未处理预约信息
     * @return
     */
    public PageInfo queryAll(String pageNum,String condition){
        if(pageNum==null||"".equals(pageNum)){
            pageNum="1";
        }
        PageHelper.startPage(Integer.parseInt(pageNum), 5);
        List<Makeanappointmentinfo> makeanappointmentinfos=null;
        try {
            makeanappointmentinfos = makeanappointmentinfoMapper.selectAll(condition);
        }catch (Exception e){
            throw new RuntimeException("查询失败");
        }
        PageInfo page = new PageInfo(makeanappointmentinfos);
        return page;
    }

    /**
     *  根据预约编号查询预约信息
     * @param makeanappointmentid
     * @return
     */
    public Makeanappointmentinfo queryMakeanAppointmentById(String makeanappointmentid){
        int makeAnAppointMentid = Integer.parseInt(makeanappointmentid);
        Makeanappointmentinfo makeanappointmentinfo = null;
        try {
            makeanappointmentinfo = makeanappointmentinfoMapper.selectByPrimaryKey(makeAnAppointMentid);
        }catch (Exception e){
            throw new RuntimeException("根据Id查询失败");
        }
        return makeanappointmentinfo;
    }

    /**
     * 查询所有服务
     * @return
     */
    public List<Servicetype> queryAllServicetype(){
        List<Servicetype> servicetypes =null;
        try {
            servicetypes = servicetypeMapper.selectAll("");
        }catch (Exception e){
            throw new RuntimeException("查询服务失败");
        }
        return servicetypes;
    }
//    /**
//     *  受理：修改受理状态
//     * @param appointmentId
//     */
//    public void updateAccept(Integer appointmentId){
//        makeanappointmentinfoMapper.updateAccept(appointmentId);
//    }
//
    /**
     *  添加预约
     * @param makeanappointmentinfo
     */
    public int addAppointment(Makeanappointmentinfo makeanappointmentinfo){
        Date date=new Date();
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=format.format(date);
        makeanappointmentinfo.setMakeanappointmenttime(time);
        int insert = makeanappointmentinfoMapper.insert(makeanappointmentinfo);
        return insert;

    }
//
    /**
     *  删除预约
     */
    public void delAppointment(String makeAnAppointMentId){
        //  转换数字
        Integer id = Integer.parseInt(makeAnAppointMentId);
        try {
            Integer updateCount = makeanappointmentinfoMapper.removeByPrimaryKey(id);
        }catch (Exception e){
            throw new RuntimeException("取消预约失败");
        }
    }
    /**
     *  修改预约
     */
    public int upAppointment(Makeanappointmentinfo makeAnAppointMent){
        int update=0;
        makeAnAppointMent.setStatus(0);
        try {
            update=makeanappointmentinfoMapper.updateByPrimaryKey(makeAnAppointMent);
        }catch (Exception e){
            throw new RuntimeException("修改预约失败");
        }
        return update;
    }
    /**
     *  修改受理状态
     */
    public void updateMakeAnAppointMentAccept(Integer makeanappointmentid){
        try {
            makeanappointmentinfoMapper.updateMakeAnAppointMentAccept(makeanappointmentid);
        }catch (Exception e){
            throw new RuntimeException("修改受理状态失败");
        }
    }
}
